package com.eastfair.venueservice.controller;

import cn.hutool.core.bean.BeanUtil;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.core.exception.BizException;
import com.eastfair.venueservice.dto.*;
import com.eastfair.venueservice.entity.SpaceAcceptance;
import com.eastfair.venueservice.service.SpaceAcceptanceService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.venueservice.vo.SpaceAcceptanceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 展厅验收记录
 * </p>
 *
 * @author linan
 * @date 2023-01-06
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/spaceAcceptance")
@Api(value = "SpaceAcceptance", tags = "展厅验收记录")
@PreAuth(replace = "venueservice:spaceAcceptance:")
public class SpaceAcceptanceController extends SuperController<SpaceAcceptanceService, Long, SpaceAcceptance, SpaceAcceptancePageQuery, SpaceAcceptanceSaveDTO, SpaceAcceptanceUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<SpaceAcceptance> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<SpaceAcceptance> spaceAcceptanceList = list.stream().map((map) -> {
            SpaceAcceptance spaceAcceptance = SpaceAcceptance.builder().build();
            //TODO 请在这里完成转换
            return spaceAcceptance;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(spaceAcceptanceList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<SpaceAcceptancePageQuery> params) {
        SpaceAcceptancePageQuery pageQuery = params.getModel();

    }


    /**
     * 提交验收申请
     * @param spaceAcceptanceDTO
     * @return
     */
    @SysLog("提交验收申请")
    @ApiOperation(value = "提交验收申请")
    @PostMapping(value = "/saveSpaceAcceptance")
    public R<Boolean> saveSpaceAcceptance(@RequestBody SpaceAcceptanceDTO spaceAcceptanceDTO){
        try {
            return R.success(baseService.saveSpaceAcceptance(spaceAcceptanceDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 修改验收申请
     * @param spaceAcceptanceDTO
     * @return
     */
    @SysLog("修改验收申请")
    @ApiOperation(value = "修改验收申请")
    @PostMapping(value = "/updateSpaceAcceptance")
    public R<SpaceAcceptance> updateSpaceAcceptance(@RequestBody SpaceAcceptanceDTO spaceAcceptanceDTO){
        try {
            return this.update(BeanUtil.copyProperties(spaceAcceptanceDTO, SpaceAcceptanceUpdateDTO.class));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 查询验收申请
     * @param query
     * @return
     */
    @ApiOperation(value = "查询验收申请")
    @PostMapping(value = "/querySpaceAcceptance")
    public R<IPage<SpaceAcceptanceVO>> querySpaceAcceptance(@RequestBody PageParams<SpaceAcceptanceQuery> query){
        try {
            return R.success(baseService.querySpaceAcceptance(query));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

}
